<template>
  <div class="app-container">
    <div class="box">
      <div id="preview" @paste="handlePaste">
        <span>将图片按Ctrl+V 粘贴至此处</span>
      </div>
      <el-button
        @click="uploadPlans"
      >上传文件</el-button>
    </div>
    <div v-if="user">
      <el-row :gutter="20">

        <el-col :span="6" :xs="24">
          <user-card :user="user" />
        </el-col>

        <el-col :span="18" :xs="24">
          <el-card>
            <el-tabs v-model="activeTab">
              <el-tab-pane label="Timeline" name="timeline">
                <timeline />
              </el-tab-pane>
              <el-tab-pane label="Account" name="account">
                <account :user="user" />
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </el-col>

      </el-row>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import UserCard from './components/UserCard'
import Timeline from './components/Timeline'
import Account from './components/Account'

export default {
  name: 'Profile',
  components: { UserCard, Timeline, Account },
  data() {
    return {
      user: {},
      activeTab: 'timeline'
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'avatar',
      'roles'
    ])
  },
  created() {
    this.getUser()
  },
  methods: {
    getUser() {
      this.user = {
        name: this.name,
        role: this.roles.join(' | '),
        email: 'admin@test.com',
        avatar: this.avatar
      }
    },
    handlePaste(event) {
      const items = (event.clipboardData || window.clipboardData).items
      let file = null

      if (!items || items.length === 0) {
        this.$message.error('当前浏览器不支持本地')
        return
      }
      console.log(items[0])
      console.log(items[0].type)
      // 搜索剪切板items
      for (let i = 0; i < items.length; i++) {
        if (items[i].type.indexOf('image') !== -1) {
          file = items[i].getAsFile()
          break
        }
      }
      if (!file) {
        this.$message.error('粘贴内容非图片')
        return
      }
      // 此时file就是我们的剪切板中的图片对象
      // 如果需要预览，可以执行下面代码
      const reader = new FileReader()
      reader.onload = event => {
        document.querySelector('#preview').innerHTML = `<img src="${event.target.result}">`
      }
      reader.readAsDataURL(file)
      this.file = file
    },
    // 上传文件成功后回调
    uploadPlans() {
      const file = this.file
      if (!file) {
        this.$message.error('请粘贴图片后上传')
        return
      }
      this.loading = true
      const form = new FormData()
      form.append('file', file)
      form.append('type', this.type)
      // uploadCertificate是封装的axios请求，自己根据需求传参
      // uploadCertificate(form)
      //   .then(data => {
      //     if (data.data && data.data.success) {
      //       this.certificate_pic = data.data.data.source
      //       this.$message.success(this.name + '上传成功！')
      //     } else {
      //       this.$message.error(this.name + '上传失败！')
      //     }
      //   }).catch(() => {})
    }
  }
}
</script>
